package com.cyandpos.pad.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.changyi.device.APApplication;
import com.cyandpos.pad.R;
import com.cyandpos.pad.common.commondata.ErpCommonData;
import com.cyandpos.pad.posdata.Goods;
import com.cyandpos.pad.querygoods.presenter.QueryGoodsPresenter;
import com.cyandpos.pad.querygoods.presenter.QueryGoodsPresenterImpl;
import com.cyandpos.pad.querygoods.view.QueryGoodsView;
import com.cyandpos.pad.sale.view.SelfNumKeyView;
import com.cyandpos.pad.utils.CommonUtils;
import com.cyandpos.pad.utils.ToastUtils;


public class QueryGoodsActivity extends Activity implements QueryGoodsView {
    private QueryGoodsPresenter mQueryGoodsPresenter;
    private ImageButton ib_back,ib_scan;
    private Button ib_querygoods;
    private EditText et_querygoods;
    private TextView tv_goodsname, tv_goodscode, tv_goodsprice, tv_goodsdiscount, tv_goodsinventory;
    private TextView tv_goodstype,tv_goodslogo,tv_dialog_name;
    private ProgressDialog dialog;
    private SelfNumKeyView sf_containt;

    private LinearLayout ll_queryGoods;
    public final int QRCODE_QueryGOODS_REQUEST = 2;
    private Toolbar back_toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_querygoods);
        APApplication.getInstance().addActivity(this);
        mQueryGoodsPresenter = new QueryGoodsPresenterImpl(this);
        dialog = new ProgressDialog(QueryGoodsActivity.this);
        dialog.setCancelable(false);
        dialog.setMessage("正在查询商品，请稍等 ....");

        initView();
    }
    private void initView() {
        ib_scan= findViewById(R.id.ib_scan);
        et_querygoods = findViewById(R.id.et_querygoods);
        ib_querygoods = findViewById(R.id.ib_select);
        tv_goodscode = findViewById(R.id.tv_goodscode);
        tv_goodstype = findViewById(R.id.tv_goodstype);
        tv_goodslogo = findViewById(R.id.tv_goodslogo);
        tv_dialog_name = findViewById(R.id.tv_dialog_title_name);
        tv_goodsdiscount = findViewById(R.id.tv_goodsdiscount);
        tv_goodsname = findViewById(R.id.tv_goodsname);
        tv_goodsprice = findViewById(R.id.tv_goodsprice);
        tv_goodsinventory = findViewById(R.id.tv_goodsinventory);
        ll_queryGoods= findViewById(R.id.ll_queryGoods);

        sf_containt = findViewById(R.id.querygoods_keyboard);
        sf_containt.setEnterReceptor(et_querygoods);
        sf_containt.setIsCanInputPoint(true);
        sf_containt.setOnCallBack(new SelfNumKeyView.SelfNumCallBack() {
            @Override
            public void keyBoardSure(String num,String hint) {
                //  ed_old_saleId.setText(num);
                // mRefundPresenter.getOldTicket(isOrderNum);
                if (CommonUtils.isNullOrEmpty(CommonUtils.getContent(et_querygoods))){
                    ToastUtils.showMsg("商品码不能为空");
                    return;
                }
                mQueryGoodsPresenter.QueryGoods(CommonUtils.getContent(et_querygoods), ErpCommonData.loadConfig.getCashierDeptId()+"");//CommonUtils.autoAdd0(

            }
        });

        CommonUtils.setNoSoftware(et_querygoods, QueryGoodsActivity.this, true);



        ib_scan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startActivityForResult(new Intent(QueryGoodsActivity.this,ScanCodeActivity.class),QRCODE_QueryGOODS_REQUEST);

            }
        });

        ib_querygoods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (CommonUtils.isNullOrEmpty(CommonUtils.getContent(et_querygoods))){
                    ToastUtils.showMsg("商品码不能为空");
                    return;
                }


                mQueryGoodsPresenter.QueryGoods(CommonUtils.getContent(et_querygoods), ErpCommonData.loadConfig.getCashierDeptId()+"");//CommonUtils.autoAdd0(

            }
        });

        tv_dialog_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 finish();
            }
        });

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_OK) {
            Toast.makeText(getApplicationContext(), "未成功", Toast.LENGTH_SHORT).show();
            return;
        }
        switch (requestCode) {
            case QRCODE_QueryGOODS_REQUEST:
                Bundle bundle = data.getExtras();
                String content = bundle.getString("Code");
                if ("".equals(content)) {
                    ToastUtils.showMsg("扫码失败");
                    return;
                }
                et_querygoods.setText(content);
                mQueryGoodsPresenter.QueryGoods(content, ErpCommonData.loadConfig.getCashierDeptId()+"");
                break;
            default:
                break;

        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 查询商品成功
     *
     * @param goods
     */
    @Override
    public void onQueryGoodsSuccess(Goods goods) {
        ll_queryGoods.setVisibility(View.VISIBLE);
        tv_goodsname.setText(goods.getName());
        tv_goodscode.setText(goods.getCode());
        tv_goodsprice.setText(goods.getPrice());
        tv_goodsdiscount.setText(goods.getDiscount());
        tv_goodsinventory.setText(goods.getCanSaleCount()+"");
//        tv_goodslogo.setText(getStatusName(goods.gets()));
        tv_goodstype.setText(goods.getClassType());

    }

    private String getStatusName(int status) {
        if (status == -1)
            return "特殊商品";
        else if(status==0)
            return "正常";

        else if(status==1)
            return "未审核";

        else if(status==2)
            return "淘汰";

        else if(status==3)
            return "新品";

        else if(status==4)
            return "暂停";

        else if(status==5)
            return "终止";
        else
            return "";

    }


    /*
     * 查询商品失败方法
     *
     * @param errorMsg
     */

    @Override
    public void showLoadFailMsg(String errorMsg) {
        Toast.makeText(this, "查询商品失败" + errorMsg, Toast.LENGTH_LONG).show();
        clearGoodsInfo();
    }

    @Override
    public void showProgress() {
        dialog.show();

    }

    @Override
    public void hideProgress() {
        dialog.dismiss();

    }

    public void clearGoodsInfo(){
        et_querygoods.setText("");
        ll_queryGoods.setVisibility(View.INVISIBLE);
    }
    @Override
    protected void onResume() {
        super.onResume();
        tv_dialog_name.setText("商品查询");
    }
}
